import {request} from '../../utils/http'

Page({
  data: {
    store: {},
    product: [],
    member: {},
    appointment_entrance:0,
    id:'',
    wechat_sn:''
  },
  onLoad(query) {
    let appointment_entrance = my.getStorageSync({key:'appointment_entrance'}).data

    // 页面加载
    let {id,wechat_sn} = query
    let options = my.getEnterOptionsSync();
    if (!id) {
      id = options.query.id;
    }

    if (!wechat_sn) {
      wechat_sn = options.query.wechat_sn
    }

    this.setData({
      appointment_entrance,
      id,
      wechat_sn
    }, () => {
      this.fetch(id)
    })
  },
  onReady() {
    // 页面加载完成
  },
  
  onShow() {
    my.getStorage({
      key: 'userInfo',
      success: (res) => {
        console.log(res)
        if (res.data) {
          this.setData({
            member: res.data,
          })
        }
      }
    });
  },
  onHide() {
    // 页面隐藏
  },
  onUnload() {
    // 页面被关闭
  },
  onTitleClick() {
    // 标题被点击
  },
  onPullDownRefresh() {
    // 页面被下拉
  },
  onReachBottom() {
    // 页面被拉到底部
  },
  onShareAppMessage() {
    // 返回自定义分享信息
    // return {
    //   title: '微蓝辅导',
    //   desc: '好辅导',
    //   path: 'pages/index/index',
    // };
  },
  fetch(id) {
    request('/wx/store_search', 'get', {
      wechat_sn:this.data.wechat_sn,
      show_channel:5
    }).then(res => {
      if (res.code == 200) {
        res.data.store['product'] = res.data.product;
        this.getConfig(res.data.store.shop.id)
        this.setData({
          store: res.data.store,
          product: res.data.product,
          hasSearch:true
        })
      }
      console.log(res)
    })
    // request('/shop/store/' + id, 'get').then(res => {
    //   if (res.code == 200) {
    //     this.getConfig(res.data.store.shop.id)

    //     this.setData({
    //       store: res.data.store,
    //       product: res.data.product
    //     })
    //   }
      
    // })
  },
  confirm(e) {
    const id = e.target.dataset.id
    const item = e.target.dataset.item;

    if (item.price_is_edit == 1) {
      my.navigateTo({ 
        url: `/pages/updateProductPrice/updateProductPrice?id=${item.id}&wechat_sn=${this.data.wechat_sn}&storeId=${this.data.store.id}`
      })

    } else {
      my.navigateTo({ url: '/pages/orderConfirm/index?id=' + id + '&storeId=' + this.data.store.id})

    }

    // if (!this.data.member.mobile) {
    //   my.navigateTo({ url: '../my/index?product_id=' + id + '&storeId=' + this.data.store.id})
    //   return;
    // }
  },
  goReservation(){
    my.navigateTo({
      url:`/pages/reservation/index/index?store_id=${this.data.id}&store_name=${this.data.store.name}`
    })
  },
  async getConfig(shop_id) {
    let res = await request('/shop/getSwitchConfigs','get', {shop_id: shop_id})
    if (res.code === 200) {
      console.log(res.data)
      // this.globalData.logo = res.data.app_logo
      // this.globalData.mobile_login = res.data.mobile_login

      my.setStorage({
        key:'supplier_id',
        data: res.data.supplier_id
      })

      my.setStorage({
        key: 'mobile_login',
        data: res.data.mobile_login,
        
      })

      my.setStorage({
        key: 'chain_initiative_pay',
        data: res.data.chain_initiative_pay,
        
      })

      my.setStorage({
        key: 'zm_initiative_pay',
        data: res.data.zm_initiative_pay,
        
      })
      

      my.setStorage({
        key: 'card_initiative_pay',
        data: res.data.card_initiative_pay,
        
      })

      my.setStorage({
        key: 'after_sales_hidden',
        data: res.data.after_sales_hidden,
        
      })

      my.setStorage({
        key: 'aiqian_zm',
        data: res.data.aiqian_zm,
        
      })

      my.setStorage({
        key: 'aiqian_card',
        data: res.data.aiqian_card,
        
      })

      my.setStorage({
        key:'kefu_show',
        data: res.data.kefu_show
      })

      my.setStorage({
        key:'card_pay_identity_no_required',
        data: res.data.card_pay_identity_no_required 
      })
      

      my.setStorage({
        key:'appointment_entrance',
        data: res.data.appointment_entrance 
      })
      
      this.setData({
        appointment_entrance: res.data.appointment_entrance
      })
    }
  }
});
